/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sioc.factory;

import com.jspx.cache.event.CacheEventListener;
import com.jspx.cache.Cache;
import com.jspx.cache.core.CacheEntry;
import com.jspx.utils.BeanUtil;
import com.jspx.utils.ClassUtil;
import com.jspx.utils.StringUtil;


/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-4-23
 * Time: 0:33:17
 */
public class LifecycEventListeners implements CacheEventListener
{
    //private static final Logger log = LoggerFactory.getLogger(LifecycEventListeners.class);

    //删除后
    public void notifyElementRemoved(final Cache cache, final CacheEntry entry)
    {
      //  log.debug("notifyElementRemoved-删除后----sioc  cache=" + cache.getName() + "  entry=" + entry.getKey());
    }

    //放入
    public void notifyElementPut(final Cache cache, final CacheEntry entry)
    {
      //  log.debug("notifyElementPut-放入----sioc  cache=" + cache.getName() + "  entry=" + entry.getKey());
    }

    //修改
    public void notifyElementUpdated(final Cache cache, final CacheEntry entry)
    {
      //  log.debug("notifyElementUpdated-修改----sioc  cache=" + cache.getName() + "  entry=" + entry.getKey());
    }

    //过期的
    public void notifyElementExpired(final Cache cache, final CacheEntry entry)
    {
      //  log.debug("notifyElementExpired-过期的----sioc  cache=" + cache.getName() + "  entry=" + entry.getKey());
    }

    //被逐出 超出范围的 删除的
    public void notifyElementEvicted(final Cache cache, final CacheEntry entry)
    {
      //  log.debug("notifyElementEvicted----sioc cache=" + cache.getName() + "  entry=" + entry.getKey());
        LifecycleObject lifecycleObject = (LifecycleObject) entry.getValue();
        if (lifecycleObject != null&&lifecycleObject.getObject()!=null && !lifecycleObject.isSingleton())
        {
            try
            {
                Object bean = lifecycleObject.getObject();
                if (ClassUtil.haveMethodsName(bean.getClass(),lifecycleObject.getDestroyMethod()))
                    BeanUtil.invoke(lifecycleObject.getObject(), lifecycleObject.getDestroyMethod());
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    //删除所有
    public void notifyRemoveAll(final Cache cache)
    {
        for (String key : cache.getKeys())
        {
            CacheEntry cacheEntry = cache.get(key);
            LifecycleObject lifecycleObject = (LifecycleObject) cacheEntry.getValue();

            if (lifecycleObject != null)
            {
                Object bean = lifecycleObject.getObject();
                try
                {
                       if (bean!=null&&!StringUtil.isNULL(lifecycleObject.getDestroyMethod())&&ClassUtil.haveMethodsName(bean.getClass(),lifecycleObject.getDestroyMethod()))
                        BeanUtil.invoke(bean,lifecycleObject.getDestroyMethod());
                } catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        }
    }

}